import {createRouter, createWebHashHistory, Router, RouteRecordRaw} from 'vue-router';
import viewsRouter from './views'
import pageRouter from './page'

// 对RouteRecordRaw类型进行扩展
export type AddRouteRecordRaw = RouteRecordRaw & {
    hidden?: boolean
}
const router:Router = createRouter({
    history: createWebHashHistory(),
    routes: [...viewsRouter, ...pageRouter] as AddRouteRecordRaw[]
})



/**
 * 路由守卫
 * to: 即将要进入的目标 用一种标准化的方式
 * from: 当前导航正要离开的路由 用一种标准化的方式
 **/
router.beforeEach((to, from, next) => {
    let token : string | null =  window.localStorage.getItem('token')
    //进入路由之前先判断是否有token，没有则回到login
    if (to.name !== 'login' && token === null) {
        next({ name: 'login' })
    }
    else {
        next()
    }
})

export default router
